/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nostra13.sample.provider;

import com.nostra13.sample.ResourceTable;
import com.nostra13.sample.bean.ImageFolder;
import com.nostra13.sample.constant.Constants;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class ImageProvider extends BaseItemProvider {

    private Context mContext;
    private ArrayList<ImageFolder> mImageFolders;
    private LayoutScatter inflater;
    private DisplayImageOptions options;
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

    public ImageProvider(Context context, ArrayList<ImageFolder> imageFolders) {
        mContext = context;
        mImageFolders = imageFolders;
        inflater = LayoutScatter.getInstance(context);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(ResourceTable.Media_ic_stub)
                .showImageForEmptyUri(ResourceTable.Media_ic_empty)
                .showImageOnFail(ResourceTable.Media_ic_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .displayer(new CircleBitmapDisplayer(Color.WHITE.getValue(), 5))
                .build();
    }

    @Override
    public int getCount() {
        return mImageFolders.size();
    }

    @Override
    public Object getItem(int i) {
        return mImageFolders.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component convertView, ComponentContainer componentContainer) {
        Component view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
            view = inflater.parse(ResourceTable.Layout_item_image_gird, componentContainer, false);
            holder = new ViewHolder();
            holder.text = (Text) view.findComponentById(ResourceTable.Id_text);
            holder.image = (Image) view.findComponentById(ResourceTable.Id_image);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.text.setText("Item " + (i + 1));
        ImageSize targetSize = new ImageSize(80, 50); // 结果PixelMap将适合此大小
        ImageLoader.getInstance().loadImage(mImageFolders.get(i).path, targetSize, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, Component view, PixelMap loadedImage) {
                // Do whatever you want with PixelMap
                holder.image.setPixelMap(loadedImage);
            }
        });

        return view;
    }

    static class ViewHolder {
        Text text;
        Image image;
    }

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, Component view, PixelMap loadedImage) {
            if (loadedImage != null) {
                Image imageView = (Image) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

}
